#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QVBoxLayout>
#include<QHBoxLayout>
#include"gamelogic.h"
#include<QMessageBox>
#include<QDebug>
#include<QTimer>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_qipan(new qipan(this)),
    seconds(0),
    gameStarted(false),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    m_logic=new GameLogic;
    int boardSize=0;
    m_qipan->getBoardInfo(boardSize);
    m_logic->setQipanBoard(m_qipan->getBoardInfo(boardSize),boardSize);

    setFixedSize(800,680);

    /*设置布局*/
    QWidget *centralWidget=new QWidget(this);
    setCentralWidget(centralWidget);
    QVBoxLayout *layout=new QVBoxLayout(centralWidget);
    layout->addWidget(m_qipan);
    // 手动设置按钮位置
        ui->start_pushbutton->setParent(centralWidget);
        ui->start_pushbutton->setGeometry(630, 60, 120, 50);
        ui->pause_pushbutton->setParent(centralWidget);
        ui->pause_pushbutton->setGeometry(630, 150, 120, 50);
        ui->renege_pushbutton->setParent(centralWidget);
        ui->renege_pushbutton->setGeometry(630, 430, 120, 50);
        ui->lcdNumber->setParent(centralWidget);
        ui->lcdNumber->setGeometry(630, 260, 120, 50);

        // 显示按钮
        ui->start_pushbutton->show();
        ui->pause_pushbutton->show();
        ui->renege_pushbutton->show();
        ui->lcdNumber->show();


    connect(m_qipan,&qipan::chessClick,this,&MainWindow::handchessClickslot);
    connect(m_logic,&GameLogic::Aiposition,this,&MainWindow::handleAichessPoslot);

    /*设置时间*/
    m_timer = new QTimer(this);
    connect(m_timer, &QTimer::timeout, this, &MainWindow::updateTime);

        // 设置按钮点击事件
    connect(ui->start_pushbutton, &QPushButton::clicked, this, &MainWindow::startTimer);
    connect(ui->pause_pushbutton, &QPushButton::clicked, this, &MainWindow::resetTimer);
    connect(ui->renege_pushbutton,&QPushButton::clicked,m_qipan,&qipan::undoMove);




}
void MainWindow::handchessClickslot(int row, int col)
{
    m_qipan->setBoardInfo(row,col,PLATER);
    m_qipan->update();
    if(m_logic->checkWin(row,col,PLATER)==true)
    {
        /*玩家获胜*/
        QMessageBox::information(this,"游戏结束","玩家获胜");
         m_timer->stop();
    }
    else{
        /*AI下棋*/
        /*什么叫最优*/
        /*1.对你自己最好的点+2.对对手最不好的点*/

       m_logic->computeMove();
        
    }

}

void MainWindow::handleAichessPoslot(int row, int col)
{
    m_qipan->setBoardInfo(row,col,COMPUTER);
    /*更新棋盘*/
    m_qipan->update();
    if(m_logic->checkWin(row,col,COMPUTER)==true)
    {
        QMessageBox::information(this,"游戏结束","人机获胜");
         m_timer->stop();
    }

}
void MainWindow::startTimer()
{
    gameStarted = true;
    emit gameStartedChanged(gameStarted);  // 游戏开始时发射信号
    // 启动定时器
    m_timer->start(1000);  // 每秒触发一次
}

void MainWindow::updateTime()
{
    // 增加秒数
       seconds++;

       // 计算分钟和秒钟
       int minutes = seconds / 60;
       int remainingSeconds = seconds % 60;

       // 格式化成 "MM:SS" 形式
       QString timeStr = QString("%1:%2")
                             .arg(minutes, 2, 10, QLatin1Char('0'))  // 分钟部分，宽度为 2，不足补 0
                             .arg(remainingSeconds, 2, 10, QLatin1Char('0'));  // 秒钟部分，宽度为 2，不足补 0

       // 更新 QLCDNumber 显示
       ui->lcdNumber->display(timeStr);
}

void MainWindow::resetTimer()
{
       gameStarted=false;
       QMessageBox::information(this, "游戏重置", "游戏已重置，棋盘已清空！");
       m_qipan->resetBoard();
        emit gameStartedChanged(gameStarted);  // 发送信号

       m_timer->stop();

       // 重置秒数为 0
       seconds = 0;

       // 更新 QLCDNumber 显示为 "00:00"
       ui->lcdNumber->display("00:00");

}



MainWindow::~MainWindow()
{
    delete ui;
}





